<template>
  <div class="body">
    <div class="table_content">
      <div class="table_filtrate">
        <div class="tf_item" style="margin-right: 30px">
          <div class="tag">时间</div>
          <el-date-picker
            size="mini"
            v-model="tableParams.time_interval"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="timestamp"
          >
          </el-date-picker>
        </div>

        <div class="tf_item">
          <el-button size="mini" @click="loadTableData" type="primary" plain
            >搜索</el-button
          >
        </div>
      </div>

      <div class="table-card">
        <el-table
          class="table"
          :data="tableData.data"
          :row-class-name="tableRowClassName"
          v-loading="loading.table"
           show-summary :summary-method="$_App_getSummaries"
          :border="true"
          size="mini"
        >
          <el-table-column prop="date" label="日期" width="100">
          </el-table-column>
          <el-table-column
            prop="advertising_id"
            label="贷超ID"
            width="80"
          ></el-table-column>
          <el-table-column
            prop="advertising_name"
            label="贷超名称"
            width="120"
          ></el-table-column>
          <el-table-column label="贷超类型" width="120">
            <template slot-scope="scope"> H5 </template>
          </el-table-column>
          <el-table-column label="结算方式" width="120">
            <template slot-scope="scope">
              <el-tag
                size="mini"
                v-if="scope.row.advertising_federated_login == 1"
                >联登</el-tag
              >
              <el-tag size="mini" v-else type="success">uv</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="advertising_cooperative_price"
            label="合作价格"
            width="120"
          >
          </el-table-column>
          <el-table-column prop="uv" label="访问数量" width="120">
          </el-table-column>
          <el-table-column prop="cost" label="预计收益" width="120">
          </el-table-column>
          <el-table-column label="更新时间" width="140">
            <template slot-scope="scope">
              <div>{{ scope.row.updatetime }}</div>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          @size-change="handleTableSizeChange"
          @current-change="handleTableCurrentChange"
          :current-page="currentPage4"
          :page-sizes="$pageSize"
          :page-size="tableParams.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { apiDashboardAdvertisinglistpage } from "@/api/apiCommon";
import { paramsDeleteNull } from "@/utils/common";
import { assignValueToList, processTableParams } from "@/utils/appUtil";
import { formatTimestamp,dateToTimestamp } from '@/utils/datetimeUtil'
export default {
  name: "Index",
  data() {
    return {
      tableParams: {
        page: 1,
        size: 15,
        time_interval: [
          new Date(new Date().setHours(0, 0, 0, 0)), // 今天的初始时间戳
          new Date(new Date().setHours(23, 59, 59, 999)), // 今天的结束时间戳
        ],
      },
      tableData: {
        current_page: 1,
        data: [],
        last_page: 1,
        per_page: "15",
        total: 0,
      },
      currentPage4: 1,
      business_select: [],
      organization_select: [],
      fromViewCofing: {
        input_width: "120px",
        label_width: "120px",
      },
      loading: {
        dialog_create: false,
        table: false,
        form: false,
      },
    };
  },
  created() {
    this.loadTableData();
  },
  methods: {
    // 列表请求
    loadTableData() {
      const _this = this;
      _this.loading_start("table");
      apiDashboardAdvertisinglistpage(_this.tableParams)
        .then((response) => {
          const result = response;
          var tableData = result.data.datalist;
          for (var i = 0; i < tableData.data.length; i++) {
            tableData.data[i]['updatetime'] = formatTimestamp(tableData.data[i]['updatetime'],'YYYY-MM-DD HH:mm:ss');
          }
          _this.tableData = tableData; 
          _this.loading_end("table");
        })
        .catch(function (result) {
          _this.tableData.data = null;
          _this.loading_end("table");
        });
      return;
      processTableParams({ ..._this.tableParams }).then((tableParamsData) => {
        console.log(tableParamsData, "tableParamsData");
        apiDashboardAdvertisinglistpage(tableParamsData)
          .then((response) => {
            const result = response;
            var datalist = result.data.datalist;
            assignValueToList(datalist.data).then((response) => {
              datalist.data = response;
              _this.tableData = datalist;
              _this.loading_end("table");
            });
          })
          .catch(function (result) {
            _this.tableData.data = null;
            _this.loading_end("table");
          });
      });
    },
    // 页面跳转
    handleToGoCreate() {
      this.$router.push({ path: "/product/create", query: { id: 0 } });
    },
    // 表格行状态
    tableRowClassName({ row, rowIndex }) {
      if (row["status"] == "stop") {
        return "discard-row";
      }
    },
    // 表格操作
    handleTableEdit(index, row) {
      this.$router.push({ path: "/product/update", query: { id: row.id } });
    },
    // 表格恢复
    handleTableRegain(index, row) {
      var _this = this;
      var params = {
        id: row.id,
        status: "normal",
      };
      _this.loading_start("table");
      apiProductUpdatePart(params).then((response) => {
        _this.loading_end("table");
        _this.loadTableData();
      });
    },
    // 表格删除
    handleTableDelete(index, row) {
      const _this = this;
      _this.loading_start("table");
      apiProductDelete({
        id: row.id,
      }).then((response) => {
        this.$message({
          message: response.msg,
          type: "success",
        });
        _this.loading_end("table");
        _this.loadTableData();
      });
    },
    // 数量改变
    handleTableSizeChange(val) {
      this.tableParams.size = val;
      this.loadTableData();
    },
    // 页数改变
    handleTableCurrentChange(val) {
      this.tableParams.page = val;
      this.loadTableData();
      // console.log(`当前页: ${val}`);
    },
    // 部分状态改变
    tableUpdateItem(item) {
      var _this = this;
      var params = {
        id: item.id,
        push_status: item.push_status,
        entry_status: item.entry_status,
      };
      _this.loading_start("table");
      apiProductUpdatePart(params).then((response) => {
        _this.loading_end("table");
      });
    },
    // 加载开始...
    loading_start(keys) {
      if (Array.isArray(keys)) {
        keys.forEach((key) => {
          this.loading[key] = true;
          setTimeout(() => {
            this.loading[key] = false;
          }, 5000);
        });
      } else {
        this.loading[keys] = true;
        setTimeout(() => {
          this.loading[keys] = false;
        }, 5000);
      }
    },
    // 加载结束...
    loading_end(keys) {
      if (Array.isArray(keys)) {
        keys.forEach((key) => {
          this.loading[key] = false;
        });
      } else {
        this.loading[keys] = false;
      }
    },
  },
};
</script>

<style lang="scss"></style>